/**
 * Created by qunxing.du on 2016/8/12.
 * Copyright 2016 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.pollution.server;

import com.alibaba.fastjson.JSON;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 *  服务端处理器
 */
public class CustomServerHandler extends ChannelInboundHandlerAdapter {

    private static Logger log = Logger.getLogger(CustomServerHandler.class);

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info(msg);
        Map<String,Object> map = JSON.parseObject((String) msg,Map.class);
        ctx.fireChannelRead(map);
    }


    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        log.error(cause);
        ctx.close();
    }
}
